package com.jiahua.admin.biz.action.inventory.image;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.jiahua.admin.biz.service.InventoryItemService;
import com.opensymphony.xwork2.ActionSupport;

public class ImageReceiveAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = -9090056333140954047L;
	InventoryItemService inventoryItemService;

	public void setInventoryItemService(InventoryItemService inventoryItemService) {
		this.inventoryItemService = inventoryItemService;
	}

	byte[] imageInByte = null;
	String imageId;

	private HttpServletRequest servletRequest;

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	public String getImageId() {
		return imageId;
	}

	public void setImageId(String imageId) {
		this.imageId = imageId;
	}

	public ImageReceiveAction() {
		// System.out.println("ImageReceiveAction");
	}

	public String execute() {
		getCustomImageInBytes();
		return SUCCESS;
	}

	public byte[] getCustomImageInBytes() {
		System.out.println("[1]>>>>>>>>imageId : " + imageId + ".");
		System.out.println("[2]>>>>>>>>imageId : " + servletRequest.getParameter("imageId") + ".");

		imageInByte = inventoryItemService.getInventoryItemById(imageId).getImage();
		return imageInByte;
	}

	public String getCustomContentType() {
		return "image/jpeg";
	}

	public String getCustomContentDisposition() {
		return "anyname.jpg";
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.servletRequest = request;
	}

}